Imports System.Security.Cryptography
Imports System.IO
Imports VB = Microsoft.VisualBasic
Imports System.Text
Imports System.Runtime.InteropServices

Namespace Security
    Public Class AD

#Region "Methods for authentication by network account"
        Private Declare Auto Function FormatMessageW Lib "kernel32" (ByVal dwFlags As Integer, ByRef lpSource As Object, ByVal dwMessageId As Integer, ByVal dwLanguageId As Integer, ByVal lpBuffer As String, ByVal nSize As Integer, ByRef Arguments As Integer) As Integer

        Public Shared Function GetLastErrorMessageDescription() As String
            Const FORMAT_MESSAGE_FROM_SYSTEM As Short = &H1000S
            Const LANG_NEUTRAL As Short = &H0S
            Dim Win32Error As Integer
            Win32Error = System.Runtime.InteropServices.Marshal.GetLastWin32Error()
            Dim Buffer As String = Space(999)
            FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, 0, Win32Error, LANG_NEUTRAL, Buffer, 999, 0)
            Return Trim(Buffer)
        End Function

        '****************************************************************************************
        ' Declare the API
        '****************************************************************************************
        Private Declare Auto Function LogonUser Lib "advapi32.dll" ( _
          ByVal lpszUsername As String, _
          ByVal lpszDomain As String, _
          ByVal lpszPassword As String, _
          ByVal dwLogonType As Integer, _
          ByVal dwLogonProvider As Integer, _
          ByRef phToken As IntPtr) As Boolean

        ' Declare the logon types as constants
        Const LOGON32_LOGON_INTERACTIVE As Long = 2
        Const LOGON32_LOGON_NETWORK As Long = 3

        ' Declare the logon providers as constants
        Const LOGON32_PROVIDER_DEFAULT As Long = 0
        Const LOGON32_PROVIDER_WINNT50 As Long = 3
        Const LOGON32_PROVIDER_WINNT40 As Long = 2
        Const LOGON32_PROVIDER_WINNT35 As Long = 1
        '****************************************************************************************
        ' Define a function to login the user
        '****************************************************************************************
        Public Shared Function ValidateLogin(ByVal Username As String, ByVal Password As String, ByVal Domain As String) As Boolean

            ' This is the token returned by the API call
            ' Look forward to a future article covering
            ' the uses of it
            Dim token As IntPtr

            ' Call the API
            If LogonUser(Username, _
                Domain, _
                Password, _
                LOGON32_LOGON_NETWORK, _
                LOGON32_PROVIDER_DEFAULT, token) = True Then

                ' Since the API returned TRUE, return TRUE to the caller
                Return True
            Else
                ' Bad credentials, return FALSE
                Return False
            End If
        End Function


#End Region
    End Class

    Public Class SQLLogin

    End Class
End Namespace

